package com.yq.controller;

import com.google.gson.Gson;
import com.sun.javafx.image.IntPixelGetter;
import com.yq.model.Result;
import com.yq.model.bo.ChangeOrderBO;
import com.yq.model.bo.OrdersByPageBO;
import com.yq.model.vo.OrderInfoVO;
import com.yq.model.vo.OrdersByPageVO;
import com.yq.model.vo.OrdersVO;
import com.yq.service.AdminOrderService;
import com.yq.service.AdminService;
import com.yq.service.impl.AdminOrderServiceImpl;
import com.yq.utils.HttpUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.ref.ReferenceQueue;
import java.util.List;

@WebServlet("/api/admin/order/*")
public class AdminOrderServlet extends HttpServlet {
    private AdminOrderService adminOrderService = new AdminOrderServiceImpl();
    private Gson gson= new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replace(request.getContextPath() + "/api/admin/order/", "");
        if ("ordersByPage".equals(op)){
            ordersByPage(request,response);
        }else if("changeOrder".equals(op)){
            changeOrder(request,response);
        }
    }

    /**
     * 修改订单的信息
     * @param request
     * @param response
     */
    private void changeOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestInfo = HttpUtils.getRequestInfo(request);
        //TODO {"id":"1998","state":1,"spec":1248,"num":2}
        ChangeOrderBO changeOrderBO=null;
        try {
             changeOrderBO = gson.fromJson(requestInfo, ChangeOrderBO.class);

        }catch (Exception e){
            response.getWriter().println(gson.toJson(Result.error("参数不合法")));
            return;
        }
        int code  = adminOrderService.changeOrder(changeOrderBO);
        Result result = null;
        if (code == 200) {
            result = Result.ok();
        } else if (code == 404) {
            result = Result.error("修改失败！！！"); }
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 订单首页信息
     * @param request
     * @param response
     */
    private void ordersByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestInfo = HttpUtils.getRequestInfo(request);
        //TODO {"state":-1,"currentPage":1,"pagesize":5,"moneyLimit1":"","moneyLimit2":"","goods":"","address":"","name":"","id":""}
        OrdersByPageBO ordersByPageBO = null;
        try {
            ordersByPageBO = gson.fromJson(requestInfo, OrdersByPageBO.class);
        }catch (Exception e){
            e.printStackTrace();
            response.getWriter().println(gson.toJson(Result.error("参数不合法")));
            return;
        }
        OrdersByPageVO ordersByPage = adminOrderService.ordersByPage(ordersByPageBO);
        response.getWriter().println(gson.toJson(Result.ok(ordersByPage)));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replace(request.getContextPath() + "/api/admin/order/", "");
        if ("deleteOrder".equals(op)){
            deleteOrder(request,response);
        }else if ("order".equals(op)){
            order(request,response);
        }
    }

    /**
     * 点击编辑，出现信息/api/admin/order/order?id=1
     * @param request
     * @param response
     */
    private void order(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        int orderId = Integer.parseInt(id);
        OrderInfoVO orderInfoVO = adminOrderService.getOrderInfo(orderId);
        response.getWriter().println(gson.toJson(Result.ok(orderInfoVO)));
    }

    /**
     * 删除订单/api/admin/order/deleteOrder?id=1
     * @param request
     * @param response
     */
    private void deleteOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        int orderId=0;
        try {
            orderId=Integer.parseInt(id);
        }catch (Exception e){
            e.printStackTrace();
            response.getWriter().println(gson.toJson(Result.error("参数不合法")));
            return;
        }
        adminOrderService.deleteOrder(orderId);
        response.getWriter().println(gson.toJson(Result.ok()));
    }
}
